package com.wn.ziroom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.ziroom.entity.Cart;
import com.wn.ziroom.entity.CartItem;
import com.wn.ziroom.entity.Cities;
import com.wn.ziroom.entity.vo.CartItemVo;
import com.wn.ziroom.entity.vo.ProductionVo;
import com.wn.ziroom.mapper.*;
import com.wn.ziroom.service.CartItemService;
import com.wn.ziroom.service.CitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-23 02:27:54
 */
@Service
public class CartItemServiceImpl extends ServiceImpl<CartItemMapper, CartItem> implements CartItemService {

    private CartItemMapper cartItemMapper;
    private CartMapper cartMapper;
    private ProductionVoMapper productionVoMapper;

    @Autowired
    public void setProductionMapper(ProductionVoMapper productionVoMapper) {
        this.productionVoMapper = productionVoMapper;
    }

    @Autowired
    public void setCartMapper(CartMapper cartMapper) {
        this.cartMapper = cartMapper;
    }

    @Autowired
    public void setCartItemMapper(CartItemMapper cartItemMapper) {
        this.cartItemMapper = cartItemMapper;
    }

    @Override
    public CartItem findByUserIdAndProId(Integer userId, Integer productionId, Integer buyCount) {
        //查看该用户购物车是否有商品
        CartItem cartItem = cartItemMapper.selectByUserId(userId);
        if (Objects.isNull(cartItem)){

            ProductionVo productionVo = productionVoMapper.selectProductionById(productionId);
            BigDecimal productionPrice = productionVo.getProductionPrice();
            BigDecimal count = new BigDecimal(buyCount);
            BigDecimal subTotal = productionPrice.multiply(count);


            CartItemVo cartItemVo = new CartItemVo();
            cartItemVo.setUserId(userId);
            cartItemVo.setProductionId(productionId);
            cartItemVo.setBuyCount(buyCount);
            cartItemVo.setSubTotal(subTotal);
            //-- 如果没有商品直接新增
            int i = cartItemMapper.insertByUserId(cartItemVo);

            Integer cartItemVoId = cartItemVo.getId();

            //-- 新增后再查一边给cart表
            CartItem item = cartItemMapper.selectByCartItemId(cartItemVoId);

            //-- cart中也要加总价
            List<CartItem> list = new ArrayList();
            list.add(item);

            Cart cart = new Cart();
            cart.setCartItemList(list);
            cart.setUserId(userId);
            BigDecimal total = cart.getTotal();
            cart.setTotal(total);
            cartMapper.insertCart(cart);

            Integer cartId = cart.getId();

            //-- 生成cart_id后再赋值给cartItem表

            int x = cartItemMapper.updateCartId(cartId);


        }else {
            //如果有商品判断商品id是否和新增的商品一样
            if (cartItem.getProductionVo().getProductionId() == productionId){
                //如果商品也一样修改购买数量 总价
                int i =cartItemMapper.updateBuyCountByUserIdAndProId(userId,productionId,buyCount);
            }else {
                //如果商品不一样给该用户新增个商品
                //int i =cartItemMapper.updateByProIdAndCount(userId,productionId,buyCount);
            }
        }
        return null;
    }
}
